<template>
	<view class="container" v-if="ProductList.length!=0">
		<view class="topSolt">
			<uni-row class="demo-uni-row" :width="720">
				<uni-col :span="8" v-for="(item,index) in soltTitle" :key="index">
					<view class="demo-uni-col dark" @tap="toggle(index)" :class="{color:toggleColor==index}">
						{{item}}
					</view>
				</uni-col>
			</uni-row>
		</view>

		<view style="margin-top:65rpx;">
			<view class="goods">
				<view class="goodsItem" v-for="(item,index) in ProductList" :key="index"
				@tap="gotoDetail(item.id)"
				>
					<view>
						<image :src='item.pic' :lazy-load="true" webp="true"></image>
					</view>
					<view class="price">
						<text class="newPrice">￥{{item.price}}</text>
						<text class="olbPrice">￥{{item.originalPrice}}</text>
					</view>
					<view>
						<text class="productTitle">
							{{item.name}}
						</text>
					</view>
				</view>
			</view>
		</view>

		<uni-load-more iconType="auto" status="loading" v-show="loadshow==false" />
		<view class="load-txt" v-show="loadshow">没有更多数据了</view>
		<ScrollTop :value="scrollTop" :ScrollTopTime="300"></ScrollTop>
	</view>
</template>

<script>
	import ScrollTop from '@/components/ScrollTop.vue'
	export default {
		components: {
			ScrollTop
		},
		data() {
			return {
				soltTitle: [
					"综合排序",
					"价格由低到高",
					"价格由高到低"
				],
				toggleColor: 0,
				ProductList: [],
				params: {
					pageNum: 1,
					pageSize: 10,
					sort: ""
				},
				loadshow: false,
				total: 0,
				scrollTop: false
			}
		},
		onLoad() {
			setTimeout(()=>{
				this.getList()
			},800)
			uni.showLoading({
				title:"加载中~"
			})
		},
		methods: {
			toggle(index) {
				this.toggleColor = index
				this.params.pageNum = 1
				this.ProductList = []
				this.loadshow = false
				if (index == 0) {
					this.params.sort = ""
					this.getList()
				} else if (index == 1) {
					this.params.sort = "asc"
					this.getList()
				} else if (index == 2) {
					this.params.sort = "desc"
					this.getList()
				}
				uni.pageScrollTo({
					scrollTop: 0,
				})
			},
			getList() {
				this.$api.home.portalProduct(this.params).then(res => {
					if (res.code == 200) {
						this.ProductList.push(...res.list)
						this.total = res.total
						uni.hideLoading()
					}
				})
			},
			gotoDetail(id){
				uni.navigateTo({
					url:`/pages/view/ProductDetail?Pid=${id}`
				})
			}
		},
		onReachBottom() {
			if (this.total == this.ProductList.length) {
				this.loadshow = true
			} else {
				setTimeout(() => {
					this.params.pageNum++
					this.getList()
				}, 800)
			}
		},
		onPullDownRefresh() {
			this.ProductList=[]
			this.getList()
			setTimeout(() => {
				uni.stopPullDownRefresh()
			}, 800)
		},
		onPageScroll(e) {
			if (e.scrollTop >= 500) {
				this.scrollTop = true
			} else {
				this.scrollTop = false
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		.topSolt {
			/* #ifdef H5 */
			position: fixed;
			top: 86rpx;
			z-index: 1000;
			/* #endif */

			/* #ifdef MP-WEIXIN */
			position: fixed;
			top: 0rpx;
			z-index: 1000;
			/* #endif */

			height: 60rpx;
			width: 100%;
			background-color: white;
			line-height: 60rpx;
			text-align: center;
			border-bottom: 1px solid gainsboro;

			.color {
				color: #cb0d0d;
				border-bottom: 2px solid #cb0d0d;
			}
		}

		.goods {
			background-color: #eeeeee;
			width: 100%;
			display: flex;
			flex-wrap: wrap;
			justify-content: space-between;

			.goodsItem {
				width: 355rpx;
				margin: 8rpx 8rpx;
				background-color: #ffffff;
				border-radius: 10rpx;
				overflow: hidden;
				box-shadow: 0px 0px 5px #e2e2e2;

				image {
					width: 100%;
					height: 200px;
				}

				.price {
					padding-left: 10rpx;

					.newPrice {
						font-size: 45rpx;
						color: #cb0d0d;
					}

					.olbPrice {
						color: gray;
						text-decoration: line-through;
					}

					text:nth-child(2) {
						margin-left: 17rpx;
					}
				}

				.shoppingsubTltie {
					padding-bottom: 30rpx;
				}

				.productTitle {
					color: gray;
				}
			}
		}

		.load-txt {
			padding: 20rpx 0;
			text-align: center;
			color: #cb0d0d;
			font-size: 24rpx;
		}
	}
</style>
